﻿using System;
using System.Collections.Generic;
using System.IO;

namespace Tzx.Tools.Payroll2Images.Exe.FilesSender
{
    /// <summary>
    /// API调用
    /// </summary>
    internal class ApiCallDemo
    {
        private static Random r2 = new Random();
        private const string text = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        /// <summary>
        /// 1.提交一行发送
        /// </summary>
        public static bool Do_SubmitSend(string filePathName, ref ApiStatus status, out string message)
        {
            if (!File.Exists(filePathName))
            {
                message = "文件不存在";
                status.status_submit_send = false;
                status.message = message;
                return false;
            }

            //文件名
            string empNameText = Path.GetFileNameWithoutExtension(filePathName);
            if (string.IsNullOrEmpty(empNameText))
            {
                message = "文件名不包含接收人名称等信息";
                return false;
            }

            //1.上传文件
            System.Threading.Thread.Sleep(r2.Next(10, 100)); //模拟耗时,提交一行
            bool upload_ok = r2.Next()%19 != 0;
            if (!upload_ok)
            {
                message = "上传文件失败" + DateTime.Now.ToString("yyyyMMddHHmmssfff");
                return false;
            }
            string media_id = string.Format("@{0}{1}{2}{3}{4}",
                text.ToCharArray()[r2.Next(0, text.Length)],
                text.ToCharArray()[r2.Next(0, text.Length)],
                text.ToCharArray()[r2.Next(0, text.Length)],
                text.ToCharArray()[r2.Next(0, text.Length)],
                text.ToCharArray()[r2.Next(0, text.Length)]
                );

            //2.发送消息
            bool success = r2.Next(0, 1000)%7 != 0;
            if (!success)
            {
                message = "发送失败" + DateTime.Now.ToString("yyyyMMddHHmmssfff");
                return false;
            }
            string task_id = r2.Next(10000, 99999).ToString();
            List<string> user_id_list = new List<string>();
            user_id_list.Add(empNameText);
            string useridss = string.Join(",", user_id_list);

            //成功
            message = "ok";
            status.status_submit_send = success; //来自API
            status.media_id = string.Format("{0}", media_id);
            status.task_id = task_id;
            status.user_idss = useridss; //拼接用户
            status.message = string.Format("{0}", message);
            return true;
        }

        /// <summary>
        /// 2.获取更新消息发送进度(一行)
        /// </summary>
        public static bool Do_GetSendProgress(ref ApiStatus status, out string message)
        {
            message = null;

            //bool success = ApiCallDemo.CallProgress(task_id, out message);
            if (r2.Next(1, 100)%9 == 0)
            {
                message = "访问api出错";
                return false;
            }

            string task_id = status.task_id;

            System.Threading.Thread.Sleep(r2.Next(10, 200)); //模拟耗时,更新发送状态一行

            //TODO:百分比percent,0~100,
            //TODO:状态status,0未开始,1处理中,2处理完毕,

            //访问API获得发送状态
            bool sendOk = r2.Next(1, 100)%7 != 0;
            status.status_send_progress = sendOk;
            if (!sendOk)
            {
                status.message = "发送中";
                status.message = String.Format("{0}", status.message);
            }
            else
            {
                status.message = "发送完成";
                status.message = String.Format("{0}", status.message);
            }

            return sendOk;
        }

        /// <summary>
        /// 3.获取一个更新消息发送结果(阅读状态等)
        /// </summary>
        public static bool Do_GetResultReadStatus(ref ApiStatus status, out string message)
        {
            message = null;

            if (r2.Next(1, 100)%9 == 0)
            {
                message = "访问api出错";
                return false;
            }

            System.Threading.Thread.Sleep(r2.Next(10, 200)); //模拟耗时,更新阅读状态一行

            //TODO:返回
            /*
{
	"errcode": 0,
	"errmsg": "0,ok",
	"send_result": {
		"errcode": 0,
		"errmsg": "ok",
		"request_id": "15sptfair1urd",
		"send_result": {
			"invalid_user_id_list": [],
			"forbidden_user_id_list": null,
			"failed_user_id_list": [],
			"read_user_id_list": [],
			"unread_user_id_list": [
				"manager7677"
			],
			"invalid_dept_id_list": [],
			"forbidden_list": []
		}
	}
}
             */

            //访问API获得阅读状态
            if (r2.Next(1, 100)%3 == 0)
            {
                status.status_result_read = false;
                status.message = "未读";
                status.message = String.Format("{0}", status.message);
            }
            else
            {
                status.status_result_read = true;
                status.message = "已读";
                status.message = String.Format("{0}", status.message);
            }

            return true;
        }

        /// <summary>
        /// 4.撤回一个消息
        /// </summary>
        public static bool Do_Recall(ref ApiStatus status, out string message)
        {
            message = null;
            if (r2.Next(1, 100)%9 == 0)
            {
                message = "【撤回】访问api出错";
                return false;
            }

            System.Threading.Thread.Sleep(r2.Next(10, 200)); //模拟耗时,撤回一行

            //TODO:errcode,errmsg

            //访问API获得阅读状态
            if (r2.Next(1, 100)%17 == 0)
            {
                status.status_recall = false;
                status.message = "撤回失败";
            }
            else
            {
                status.status_recall = true;
                status.message = "已撤回";
            }

            return true;
        }

    }

}